package org.example;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class Content implements Aggregate{
    List<String> items = new ArrayList<>();

    public Content(){
        try(FileReader reader = new FileReader("Iterator/src/main/resources/test.txt")) {
            char[] buffer = new char[10];
            while (reader.read(buffer) > 0){
                items.add(new String(buffer));
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public List<String> getItems() {
        return items;
    }
    public void setItems(List<String> items) {
        this.items = items;
    }

    @Override
    public Iterator createIterator() {
        return new PageIterator();
    }

    private class PageIterator implements Iterator<String>{

        // 以页为单位进行存储
        LinkedList<List<String>> pages = new LinkedList<>();
        // 游标
        int cursor = 0;
        // 页的大小
        int limit = 10;

        PageIterator() {
            // 根据页面的大小进行分页
            int index = 0;
            List<String> page = new ArrayList<>();
            for (String item : items){
                if(index++ == limit){
                    index = 1;
                    pages.add(new ArrayList<String>(page));
                    page.clear();
                }
                page.add(item);
            }
            pages.add(new ArrayList<String>(page));
        }


        @Override
        public void display() {
            while (this.hashNext()){
                System.out.println("-----第"+(cursor+1)+"页-------");
                int index = 1;
                for (String item : pages.get(cursor++)){
                    System.out.println((index++)+"  " + item);
                }
                System.out.println();
            }
            cursor = 0;
        }

        public List<String> getPageByCursor(int cursor) {
            this.cursor = cursor;
            if (!hashNext() || this.cursor < 0) {
                System.out.println("没有该页");
                this.cursor = 0;
                return null;
            }

            System.out.println("-----第"+(cursor+1)+"页-------");
            int index = 1;
            for (String item : pages.get(cursor)){
                System.out.println((index++)+"  " + item);
            }
            System.out.println();
            return pages.get(cursor);
        }
        @Override
        public List<String> getPage(int page) {
            return getPageByCursor(page-1);
        }

        @Override
        public List<String> previousPage() {
            return getPageByCursor(cursor-1);
        }

        @Override
        public List<String> nextPage() {
            return getPageByCursor(cursor+1);
        }
        @Override
        public boolean hashNext() {
            return cursor < pages.size();
        }
    }
}
